#!/usr/bin/env python
from xml.dom.minidom import getDOMImplementation

TypeOfXml = ['flat','peak']
MaxNumberOfNode = 20
class XmlConstruct:
    XmlDocument = None
    def __init__(self, node_name, attribute_name, attribute_value):        
        impl = getDOMImplementation()
        self.XmlDocument = impl.createDocument(None, "Root", None)
        self.Root = self.XmlDocument.documentElement
        self.Storer = self.AddNodeWithAttrib(node_name, attribute_name, attribute_value)
        
        self.NumberOfNodes =  random.randint(1, MaxNumberOfNode)
        self.NumberOfStorerNode = random.randint(1, self.NumberOfNodes )
        #TypeOfConstructedXml = random.shuffle(TypeOfXml)
        self.TypeOfConstructedXml = random.choice(TypeOfXml)
    def Construct(self):
        for i in range(self.NumberOfNodes):
            if i == self.NumberOfStorerNode :
                Node = self.Storer
            else:
                Node = self.AddNodeWithAttrib('Node'+str(i), 'name', str(i))

            if i == 0 :
                self.Root.appendChild( Node )
                if self.TypeOfConstructedXml == 'flat':
                    self.CurNode = self.Root
                elif self.TypeOfConstructedXml == 'peak':
                    self.CurNode = Node
            else :
                self.CurNode.appendChild( Node )
                if self.TypeOfConstructedXml == 'peak':
                    self.CurNode = Node
                
    def AddNodeWithAttrib( self, node_name, attribute_name, attribute_value):
        node = self.XmlDocument.createElement(node_name)
        node.setAttribute(attribute_name, attribute_value)
        return node
        
    def __str__(self):
        return self.XmlDocument.toxml("windows-1251")

import sys
import threading
import random

class XmlCreator(threading.Thread):
    def __init__(self):
        pass
        threading.Thread.__init__(self)
    def run(self):
        xml = XmlConstruct("Storerkey", "name", "EEX")
        f = open("/test.xml", "w")
        f.write( str(xml) )
    
if __name__ == '__main__':
   xml = XmlConstruct("Storerkey", "name", "EEX")
   xml.Construct()
   print str(xml)
